#!/bin/sh

GRI_AUTHD=0
GRI_BACKEND=0
GRI_AUTHD_CONFIG=""

if [ -r /etc/rc.config.d/gri_backend ]
then
	. /etc/rc.config.d/gri_backend
elif [ -r /etc/gri_backend.conf ]
then
	. /etc/gri_backend.conf
elif [ -r /etc/gri_backend ]
then
	. /etc/gri_backend
fi

if [ x$1 = x ]
then
	arg=start
else
	arg=$1
fi

cd /opt/gri_backend/bin

if [ $arg = start ]
then
	[ $GRI_AUTHD -eq 0 ] && exit 0
	GRI_BACKEND_DIR=/opt/gri_backend
	export GRI_BACKEND_DIR
	args=""
	if [ -n "$GRI_AUTHD_CONFIG" ]
	then
		args="--config $GRI_AUTHD_CONFIG"
	fi

	eval /opt/gri_backend/bin/gri_authd $args
	exit $?
fi

if [ $arg = stop ]
then
	[ $GRI_AUTHD -eq 0 ] && exit 0
	x=`ps -ef | grep "/usr/bin/perl -w /opt/gri_backend/bin/gri_authd" | grep -v grep | awk '{print $2}'`
	[ -n "$x" ] && kill $x
	exit 0
fi

if [ $arg = start_msg ]
then
	[ $GRI_AUTHD -eq 0 ] && exit 0
	echo "Starting GRI backend Daemon"
	exit 0
fi

if [ $arg = stop_msg ]
then
	[ $GRI_AUTHD -eq 0 ] && exit 0
	echo "Stopping GRI backend Daemon"
	exit 0
fi

echo "usage: $0 { start | stop | start_msg | stop_msg }"
exit 1
